"""
@file:   login
@author: FrankYi
@date:   2020/08/17
@desc:
"""
from wtforms import Form, StringField,PasswordField
from wtforms.validators import DataRequired, Email, Regexp, ValidationError
from models.user import UserSouth
from werkzeug.security import check_password_hash
from libs.statuscode import FormValidateException

class LoginForm(Form):
    email = StringField(validators=[DataRequired(), Email(message='用户名格式错误')])
    # 必填，大写，小写，数字，下划线组成的6~18位的字符串
    password = PasswordField(validators=[DataRequired()])

    def validate(self):
        # 验证用户名密码
        super().validate()

        user = UserSouth.query.filter_by(useremail=self.email.data).first()
        if user and user.password == self.password.data:
            return user
        else:
            raise FormValidateException(message="用户名或密码错误")
